package io.kljf.boot.modules.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.kljf.boot.modules.security.service.IShiroService;
import io.kljf.boot.modules.sys.dao.MenuDao;
import io.kljf.boot.modules.sys.dao.UserDao;
import io.kljf.boot.modules.sys.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author katherine
 * @version 2020-12-01
 **/
@Service
public class ShiroServiceImpl implements IShiroService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public UserEntity getUserByUserName(String username) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        UserEntity user = userDao.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Set<String> getUserPermissions(UserEntity user) {
        List<String> permissionList;
        // TODO 如果是系统管理员，拥有最高权限
        permissionList = menuDao.getUserPermissionsList(user);
        Set<String> permissionSet = new HashSet<>();
        for(String per:permissionList){
            if(StringUtils.isNotEmpty(per)) {
                permissionSet.addAll(Arrays.asList(per.split(",")));
            }
        }
        return permissionSet;
    }
}
